import java.util.Scanner;

public class Cbactria
{
	public static void main(String[] args)
	{
		int n,begin_month,begin_day,end_month,end_day;
		int[] day={0,31,28,31,30,31,30,31,31,30,31,30,31};
		long number;
		Scanner input=new Scanner(System.in);
		n=input.nextInt();
		for (; n > 0 ; n--) {
			begin_month = input.nextInt();
			begin_day = input.nextInt();
			number = input.nextLong();
			end_month = input.nextInt();
			end_day = input.nextInt();
			if(begin_month == end_month){
				for (int i = begin_day; i < end_day ; i++) {
					number *= 2;
				}
			}
			else{
				for (int i = begin_day; i < day[begin_month] ; i++) {
					number *= 2;
				}
				for (int i = begin_month+1; i < end_month ; i++) {
					for (int j = 1; j <= day[i] ; j++) {
						number *= 2;
					}
				}
				for (int i = 1; i <= end_day ; i++) {
					number *= 2;
				}
			}
			System.out.println(number);
		}
	}
}